package org.lyl.taobao.controller;

import org.lyl.taobao.api.ICommodityService;
import org.lyl.taobao.api.entity.Commodity;
import org.lyl.taobao.service.CommodityServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: Re
 * @Date: 2021/4/2 17:52
 */
@WebServlet("/addCommodity")
public class AddCommodityServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(AddCommodityServlet.class);
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String c_Name = req.getParameter("c_Name");
        System.out.println(c_Name);
        String c_Kind = req.getParameter("c_Kind");
        String c_price = req.getParameter("c_Price");
        Integer c_UserId = Integer.parseInt(req.getParameter("userId"));
        float price = Float.valueOf(c_price);
        Commodity commodity = new Commodity(c_Name,c_Kind,price,c_UserId);
        logger.info(commodity.toString());
        ICommodityService iCommodityService = new CommodityServiceImpl();
        boolean judgment = iCommodityService.addCommodity(commodity);
        if (judgment) {
            req.setAttribute("msg","商品添加成功");
        } else {
            req.setAttribute("msg","商品添加失败");
        }
        req.getRequestDispatcher("checkCommodity.jsp").forward(req,resp);
    }
}
